/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAL;

import DAL.DAO.EmployeDAO;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 *
 * @author olivierm
 */
public abstract class AbstractFactory {
    
    private static AbstractFactory instance;
    
    public static void setInstance(AbstractFactory factory) {
            if (instance != null) // supprimer ce test si l'instance peut varier
        throw new RuntimeException("instance déjà enregistrée");
            instance = factory;
    }

    public static AbstractFactory getInstance() {
            if (instance == null)
                    throw new RuntimeException("il faut d'abord enregistrer une factory");
            return instance;
    }

    public static void setInstance(Class factory) {
            if (!childOfFactory(factory))
                throw new RuntimeException();
            Method m = null;

            try {
                m = factory.getDeclaredMethod("enregistrer", new Class[0]);
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                throw new InternalError("il faut une méthode enregistrer");
            }
            try {
                m.invoke(null,new Object[0]);
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                throw new InternalError("il faut une méthode enregistrer public");
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
            private static boolean childOfFactory(Class factory) {
            Class parent = factory;
            while ((parent = parent.getSuperclass()) != Object.class)
                if (parent == AbstractFactory.class) return true;
            return false;
        }    

/*
 * Introduisez ici l'accès aux classes DAO
 * ex: public absctract MaClasseDao getMaClasseDao();
 */            
    public abstract EmployeDAO getEmployeDao();
}